package base.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;

public class BsFieldTag extends BsBaseTag {
	
	private static final long serialVersionUID = 1L;

	private String fieldWidth = "";
	private String labelWidth = "";
	private String fieldSize = "";
	private String fieldType = "";
	private String placeholder = "";
	private String mutilSelect = "false";
	private String searchVisible = "false";
	private String dataUrl = "";
    private String hiddenField = "";
    private String triggerField = "id,text";
	
	@Override
	public int doStartTag() throws JspException {
		
		return Tag.EVAL_PAGE;
	}
	
	@Override
	public int doEndTag() throws JspException {
		
		//局部变量   配对于类里面变量
		String flabelWidth = labelWidth;
		String ffieldWidth = fieldWidth;
		String fplaceholder = placeholder;
		String fwidth = width;
		String fmutilSelect = "";
		String fsearchVisible = "";
		String[] ftriggerField = triggerField.split(",");
        //局部变量
		String labelClass = "control-label";
		String fieldClass = "";
		String fieldStyle = "";
		String labelStyle= "";
		String parentName = "";
		String attr = "";
		if(this.getParent().getParent() == null) 
			parentName = this.getParent().getClass().getName();
		else
		    parentName = this.getParent().getParent().getClass().getName();
		
		if (parentName.compareTo("base.tag.BsFormTag") == 0)
		{
			BsFormTag form = (BsFormTag)this.getParent().getParent();
			if(labelWidth.compareTo("") == 0)
				flabelWidth = form.getLabelWidth();
			if(fieldWidth.compareTo("") == 0)
				ffieldWidth = form.getFieldWidth();
			
			if (flabelWidth.compareTo("") == 0)
				flabelWidth = "1";
			if (ffieldWidth.compareTo("") == 0)
				ffieldWidth = "3";
			labelClass = "control-label col-xs-" + flabelWidth;
			labelStyle = "padding-left:5px;padding-right:0px;";
			fieldStyle = "padding-left:10px;padding-right:5px;";
			fieldClass = " class='col-xs-" + ffieldWidth + "' ";
		}
		if(parentName.compareTo("base.tag.BsToolBarTag") == 0 || parentName.compareTo("base.tag.BsNavBarTag") == 0)
		{
			labelClass = "control-label";
			if (fwidth.compareTo("") == 0)
			fwidth = "160";
			fieldStyle = "padding-left:5px;padding-right:5px;width:" + fwidth + "px;";
			labelStyle = "margin-left:10px;margin-right:10px;";
			fieldClass = "class='form-group' style='padding-right:10px;'";
		}
		if (placeholder.compareTo("") == 0)
			fplaceholder = caption;
		
		StringBuffer tag = new StringBuffer();
		tag.append("<label for=\"" + name + "\" class=\"" + labelClass + "\" style='" + labelStyle + "'>" + caption + "</label>\n");
		
		tag.append("<div " + fieldClass + " id='divcol" + name + "'>\n");
        
		if (fieldType.compareTo("ftText") == 0)
		{
			tag.append("<input type=\"text\" class=\"form-control input xs\" style='" + fieldStyle + "' " 
					 + " id=\"" + name + "\" name=\"" + name + "\" placeholder=\"" + fplaceholder +"\">\n");
		}
		if (fieldType.compareTo("ftEmail") == 0)
		{
			tag.append("<input type=\"email\" class=\"form-control input xs\" style='" + fieldStyle + "' " 
		             + " id=\"" + name + "\" name=\"" + name + "\" placeholder=\"" + fplaceholder +"\">\n");
		}
		if (fieldType.compareTo("ftPassword") == 0)
		{
			tag.append("<input type=\"password\" class=\"form-control input xs\" style='" + fieldStyle + "' "  
		             + " id=\"" + name + "\" name=\"" + name + "\" placeholder=\"" + fplaceholder +"\">\n");
		}
		if (fieldType.compareTo("ftArea") == 0)
		{
			tag.append("<textarea class=\"form-control input xs\" rows='3' style='" + fieldStyle + "' " 
					 + " id=\"" + name + "\" name=\"" + name + "\" placeholder=\"" + fplaceholder +"\"></textarea>\n");
		}
		if (fieldType.compareTo("ftFile") == 0)
		{
			if (mutilSelect.compareTo("true") == 0)
				fmutilSelect = " multiple ";
			tag.append("<input type=\"file\" class=\"file-loading\" " + fmutilSelect + " style='" + fieldStyle + "' " 
					 + " id=\"" + name + "\" name=\"" + name + "\" placeholder=\"" + fplaceholder +"\">\n");

			tag.append("<script type=\"text/javascript\">\n");
			tag.append("bindFile('" + name + "','" + this.dataUrl + "');\n");
			tag.append("</script>\n");
		}
		if (fieldType.compareTo("ftHtml") == 0)
		{
			tag.append("<div style='" + fieldStyle + "' " 
					 + " id=\"" + name + "\" name=\"" + name + "\"></div>\n");

			tag.append("<script type=\"text/javascript\">\n");
			tag.append("bindHtml('" + name + "','" + this.dataUrl + "');\n");
			tag.append("</script>\n");
		}

		if (fieldType.compareTo("ftCombox") == 0)
		{
			if (mutilSelect.compareTo("true") == 0)
				fmutilSelect = " multiple=true ";
			if (searchVisible.compareTo("true") == 0)
				fsearchVisible = " data-live-search=\"true\" ";
			if (parentName.compareTo("base.tag.BsToolBarTag") == 0)
				attr = " data-width='" + fwidth + "px' ";
			tag.append("<select class=\"form-control selectpicker\" " + attr  + fmutilSelect + fsearchVisible
		             + " id=\"" + name + "\" name=\"" + name + "\" placeholder=\"" + fplaceholder +"\">\n");
			tag.append("</select>\n");

			tag.append("<script type=\"text/javascript\">\n");
			tag.append("bindComboxData('" + name + "','" + dataUrl + "', '" + ftriggerField[0] + "', '" + ftriggerField[1] + "');\n");
					
			tag.append("</script>\n");
		}
		if (fieldType.compareTo("ftDate") == 0)
		{
			tag.append("<div class=\"input-group date form_date\" id=\"dt" + name + "\" data-date-format=\"yyyy-mm-dd\">\n");
			tag.append("<input class=\"form-control\" size=\"16\" id=\"" + name + "\" name=\"" + name + "\" type=\"text\" value=\"\">\n");
			tag.append("<span class=\"input-group-addon\"><span class=\"glyphicon glyphicon-calendar\"></span></span>\n");
			tag.append("</div>\n");

			tag.append("<script type=\"text/javascript\">\n");
			tag.append("bindDate('dt" + name + "');\n");
			tag.append("</script>\n");
		}

		if (fieldType.compareTo("ftComboxTree") == 0)
		{
		    tag.append("<input type='hidden' id='" + hiddenField + "' name='" + hiddenField +"'>");
			tag.append("<input type=\"text\" class=\"form-control input xs\" style='" + fieldStyle + "' " 
					 + " id=\"" + name + "\" name=\"" + name + "\" placeholder=\"" + fplaceholder +"\">\n");
			tag.append("<div id=\"divpanel" + name + "\"  class=\"panel panel-info\" style=\"position: absolute;z-index:5556;\">\n");
			tag.append("<div id=\"tree" + name + "\" class=\"treeview\"></div>\n");
			tag.append("<button style='float:right;' type='button' class='btn btn-info input-sm' onclick=\"$('#divpanel" + name + "').hide();\">关闭</button>\n");
			tag.append("</div>\n");
			
			tag.append("<script type=\"text/javascript\">\n");
			tag.append("$('#divpanel" + name + "').hide();\n");
			tag.append("$('#" + name + "').focus(function(){$('#divpanel" + name + "').width($('#" + name + "').width() + 15);"
					                                     + "$('#divpanel" + name +"').show();});\n");
			tag.append("bindTreeData('tree" + name + "','" + dataUrl + "', 'middle'," + mutilSelect + ");\n");
			if(mutilSelect.compareTo("false") == 0)
				tag.append("function do" + name + "select(node)\n"
						 + "{$('#" + name + "').val(node['" + ftriggerField[1] + "']);\n"
						 + " $('#" + hiddenField + "').val(node['" + ftriggerField[0] + "']);\n"
						 + " $('#divpanel" + name + "').hide();}\n"
						 + "bindTreeEvent('tree" + name +"', 'select', do" + name + "select);\n");
			else
				tag.append("function do" + name + "change(nodes)\n"
						 + "{var ids=[],texts=[];\n"
						 + " for(var i=0; i<nodes.length; i++)"
						 + " {ids.push(nodes[i]['" + ftriggerField[0] + "']);\n"
						 + "  texts.push(nodes[i]['" + ftriggerField[1] + "']);\n"
						 + "  $('#" + name + "').val(texts);\n"
						 + "  $('#" + hiddenField + "').val(ids);"
						 + " }\n"
						 + "}\n"
						 + "bindTreeEvent('tree" + name +"', 'change', do" + name + "change);\n");
			tag.append("$('body').click(function(evt) {\n"
	                  + "if ($(evt.target).parents('#divpanel" + name + "').length == 0 &&"
	                  + "    $(evt.target).parents('#divcol" + name + "').length == 0 && "
	                  + "    $(evt.target).id != 'divpanel" + name + "' && "
	                  + "    $(evt.target).id != '" + name + "' && "
	                  + "    $(evt.target).id != 'tree" + name + "')\n"
	                  + "$('#divpanel" + name + "').hide();\n"
	                  + "})\n");	
			tag.append("</script>\n");
		}
		if (fieldType.compareTo("ftComboxGrid") == 0)
		{
		    tag.append("<input type='hidden' id='" + hiddenField + "' name='" + hiddenField +"'>");
			tag.append("<input type=\"text\" class=\"form-control input xs\" style='" + fieldStyle + "' " 
					 + " id=\"" + name + "\" name=\"" + name + "\" placeholder=\"" + fplaceholder +"\">\n");
			tag.append("<div id=\"divpanel" + name + "\"  class=\"panel panel-info\" style=\"position: absolute;z-index:5555;\">\n");
			tag.append("<div class='btn-toolbar' id='btnbar" + name + "' role='toolbar'>\n");
			tag.append("<button  type='button' class='btn btn-info input-sm' onclick=\"$('#divpanel" + name + "').hide();\">关闭</button>\n");
			tag.append("</div>\n");
			tag.append("<table class='table table-condensed' data-search=true data-click-to-select=true  data-search-on-enter-key=true data-toolbar='#btnbar" + name + "' data-height=200 id='grid" + name + "' data-url='" + dataUrl  + "'>\n");
			tag.append("<thead>\n");
			if(mutilSelect.compareTo("true") == 0)
				tag.append("<th data-checkbox=true></th>\n");
			else
				tag.append("<th data-radio=true></th>\n");
			tag.append("<th data-valign='middle' data-align='center' data-field='" + ftriggerField[0].trim() + "' data-width=60>编号</th>\n");
			for(int i = 1; i < ftriggerField.length; i++)
				tag.append("<th data-valign='middle' data-align='center' data-field='" + ftriggerField[i].trim() + "'>编号</th>\n");
			tag.append("</thead>\n");
			tag.append("</table>\n");
			tag.append("</div>\n");

			tag.append("<script type=\"text/javascript\">\n");
			tag.append("$('#divpanel" + name + "').hide();\n");
			tag.append("$('#" + name + "').focus(function(){$('#divpanel" + name + "').width($('#" + name + "').width() + 15);"
					                                     + "$('#divpanel" + name +"').show();});\n");
			tag.append("bindGridData('grid" + name + "');\n");
			if(mutilSelect.compareTo("true") == 0)
			{
				tag.append("function do" + name + "check(row, rows)\n"
						 + "{var ids=[],texts=[];\n"
						 + " for(var i=0; i<rows.length; i++)"
						 + " {ids.push(rows[i]['" + ftriggerField[0] + "']);\n"
						 + "  texts.push(rows[i]['" + ftriggerField[1] + "']);\n"
						 + "  $('#" + name + "').val(texts);\n"
						 + "  $('#" + hiddenField + "').val(ids);"
						 + " }\n"
						 + "}\n"
						 + "bindGridEvent('grid" + name +"', 'rowcheck', do" + name + "check);\n");
				tag.append("function do" + name + "uncheck(row, rows)\n"
						 + "{var ids=[],texts=[];\n"
						 + " for(var i=0; i<rows.length; i++)"
						 + " {ids.push(rows[i]['" + ftriggerField[0] + "']);\n"
						 + "  texts.push(rows[i]['" + ftriggerField[1] + "']);\n"
						 + "  $('#" + name + "').val(texts);\n"
						 + "  $('#" + hiddenField + "').val(ids);"
						 + " }\n"
						 + "}\n"
						 + "bindGridEvent('grid" + name +"', 'rowuncheck', do" + name + "uncheck);\n");
			}else
			{	
				tag.append("function do" + name + "row(tr, row, field)\n"
						 + "{$('#" + name + "').val(row['" + ftriggerField[1] + "']);\n"
						 + " $('#" + hiddenField + "').val(row['" + ftriggerField[0] + "']);\n"
						 + " $('#divpanel" + name + "').hide();\n"
						 + "}\n"
						 + "bindGridEvent('grid" + name +"', 'rowclick', do" + name + "row);\n");
			}
			tag.append("$('body').click(function(evt) {\n"
	                  + "if ($(evt.target).parents('#divpanel" + name + "').length == 0 &&"
	                  + "    $(evt.target).parents('#divcol" + name + "').length == 0 && "
	                  + "    $(evt.target).id != 'divpanel" + name + "' && "
	                  + "    $(evt.target).id != '" + name + "' && "
	                  + "    $(evt.target).id != 'grid" + name + "')\n"
	                  + "$('#divpanel" + name + "').hide();\n"
	                  + "})\n");	
			tag.append("</script>\n");
			
		}
		tag.append("</div>");
		output(tag);
		return Tag.EVAL_PAGE;
	}
		
	public String getFieldWidth() {
		return fieldWidth;
	}

	public void setFieldWidth(String fieldWidth) {
		this.fieldWidth = fieldWidth;
	}

	public String getFieldSize() {
		return fieldSize;
	}

	public void setFieldSize(String fieldSize) {
		this.fieldSize = fieldSize;
	}

	public String getLabelWidth() {
		return labelWidth;
	}

	public void setLabelWidth(String labelWidth) {
		this.labelWidth = labelWidth;
	}

	public String getFieldType() {
		return fieldType;
	}

	public void setFieldType(String fieldType) {
		this.fieldType = fieldType;
	}

	public String getPlaceholder() {
		return placeholder;
	}

	public void setPlaceholder(String placeholder) {
		this.placeholder = placeholder;
	}

	public String getMutilSelect() {
		return mutilSelect;
	}

	public void setMutilSelect(String mutilSelect) {
		this.mutilSelect = mutilSelect;
	}

	public String getSearchVisible() {
		return searchVisible;
	}

	public void setSearchVisible(String searchVisible) {
		this.searchVisible = searchVisible;
	}

	public String getDataUrl() {
		return dataUrl;
	}

	public void setDataUrl(String dataUrl) {
		this.dataUrl = dataUrl;
	}

	public String getHiddenField() {
		return hiddenField;
	}

	public void setHiddenField(String hiddenField) {
		this.hiddenField = hiddenField;
	}

	public String getTriggerField() {
		return triggerField;
	}

	public void setTriggerField(String triggerField) {
		this.triggerField = triggerField;
	}


}
